﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mmoss.configuration;
using mmoss.orm;
using mmoss.application.classes;

namespace mmoss.application
{
    public partial class frmDockPatients : Form
    {
        public frmDockPatients()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmDockPatients_Load);
        }

        void frmDockPatients_Load(object sender, EventArgs e)
        {
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Enabled = true;
            timer1.Start();
            //bind events
            dgPatientsWaiting.CellMouseClick += new DataGridViewCellMouseEventHandler(dgPatientsWaiting_CellMouseClick);
            dgPatientsPre.CellMouseClick += new DataGridViewCellMouseEventHandler(dgPatientsPre_CellMouseClick);
            //load the list of patients in queue
            mmossEvents.PatientCheckIn += new mmossEvents.checkinPatient(mmossEvents_PatientCheckIn);
            dgPatientList.CellDoubleClick += new DataGridViewCellEventHandler(dgPatientList_CellDoubleClick);
            txtPatientNameSearch.KeyUp += new KeyEventHandler(txtPatientNameSearch_KeyUp);
            txtPatientNameSearch.Focus();

            dgPatientsPre.AutoGenerateColumns = false;
            dgPatientsWaiting.AutoGenerateColumns = false;
            dgPatientList.AutoGenerateColumns = false;
            //bind patient checking out
            UpdateList();

        }

        void timer1_Tick(object sender, EventArgs e)
        {
            UpdateList();
        }
        public void UpdateList()
        {
            //bind patients waiting
            dgPatientsWaiting.DataSource = dbConn.db.PatientCheckIns.GetCheckin(Default.CurrentLocation.Id);
            dgPatientsPre.DataSource = dbConn.db.PatientCheckIns.GetCheckOutPre(Default.CurrentLocation.Id);
        }
        void dgPatientsPre_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            PatientCheckIn l_item = ((PatientCheckIn)dgPatientsPre.Rows[e.RowIndex].DataBoundItem);
            ((frmContainer)this.MdiParent).launchSale(l_item);
        }

        void dgPatientsWaiting_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (MessageBox.Show("Move to PreCheckout?","Confirm",MessageBoxButtons.YesNo)==DialogResult.Yes)
            {
                //move to precheckout and bring up cart
                PatientCheckIn l_item = ((PatientCheckIn)dgPatientsWaiting.Rows[e.RowIndex].DataBoundItem);
                l_item.checkOutPre = DateTime.Now;
                l_item.User  = Default.CurrentUser;
                dbConn.db.SubmitChanges();
                //fire change trigger to reload patients
                UpdateList();
                //todo - wcf this service
                ((frmContainer)this.MdiParent).launchSale(l_item);
            }

        }

        void dgPatientList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            MessageBox.Show("doubleclick");
        }

        void txtPatientNameSearch_KeyUp(object sender, KeyEventArgs e)
        {
            List<Patient> l_searchResult = dbConn.db.Patients.Get(
            string.Empty
            , ((TextBox)sender).Text
            , string.Empty
            , string.Empty
            , null
            , null
            );
            dgPatientList.DataSource = l_searchResult;
        }

        void mmossEvents_PatientCheckIn(Patient l_patient)
        {
            //listener for new patient checking in
            dgPatientsWaiting.DataSource = Default.LoggedInPatients;

        }

        private void btnCheckin_Click(object sender, EventArgs e)
        {
            //checkin selected patient
            if (dgPatientList.SelectedCells[0] == null)
            {
                MessageBox.Show("Must select patient");
                return;
            }
            Patient l_patient = ((Patient)dgPatientList.Rows[dgPatientList.SelectedCells[0].RowIndex].DataBoundItem);
            PatientCheckIn l_checkin = new PatientCheckIn();
            l_checkin.Patient = l_patient;

            l_checkin.locationID = Default.CurrentLocation.Id;
            l_checkin.User = Default.CurrentUser;
            dbConn.db.PatientCheckIns.InsertOnSubmit(l_checkin);
            dbConn.db.SubmitChanges();

            //fire event to rebind items in patient checkin
        }
        
    }
}
